/**
 * 
 */
package us.gaaoc.framework.webapp.backingbeans.viewOnly;

import java.util.List;
import java.util.Map;

import javax.faces.component.UIParameter;
import javax.faces.component.UISelectItem;
import javax.faces.component.html.HtmlSelectOneListbox;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import us.gaaoc.framework.businessmodel.Filing;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.backingbeans.caseTypes.CaseRecordType;
import us.gaaoc.framework.webapp.backingbeans.profile.Links;

/**
 * @author tapshals
 * 
 */
@SuppressWarnings("unchecked")
public class MainPage extends BaseBackingBean {

	/**
	 * Properties
	 */
	
	/** Generated Serial Version UID */
	private static final long serialVersionUID = -8230174534851502295L;
	
	private String caseType = "ChildEnforcement";
	private HtmlSelectOneListbox caseTypeUI;
	private CaseRecordType caseRecordType;
	private int transmissionErrorCount;	
	
	private Filing filing;
	private UIParameter caseDetailsPageClicked;
	
	private DataModel dataTableList;
	
	private String courtUuid;
		
	/**
	 * Constructor
	 *
	 */
	public MainPage(){
		
		courtUuid = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("courtUuid");
		
		this.caseTypeUI = new HtmlSelectOneListbox();
		caseRecordType = new CaseRecordType();
		for (Links links : this.caseRecordType.getMenu()) {
			SelectItem item = new SelectItem(links.getNavigationRule(), links.getText());
            UISelectItem uiItem = new UISelectItem();
            uiItem.setValue( item );
            
            this.caseTypeUI.getChildren().add( uiItem );
		}
		
		List  cr2 = this.getModelFacade().findErrorFilings();
		this.setTransmissionErrorCount(cr2.size());

		dataTableList = new ListDataModel();
		this.loadData();
		
	}
 
	/**
	 * @return Returns the caseDetailsPageClicked.
	 */
	public UIParameter getCaseDetailsPageClicked() {
		return caseDetailsPageClicked;
	}

	/**
	 * @param caseDetailsPageClicked The caseDetailsPageClicked to set.
	 */
	public void setCaseDetailsPageClicked(UIParameter caseDetailsPageClicked) {
		this.caseDetailsPageClicked = caseDetailsPageClicked;
	}

	/**
	 * @return Returns the dataTableList.
	 */
	public DataModel getDataTableList() {
		return dataTableList;
	}

	/**
	 * @param dataTableList The dataTableList to set.
	 */
	public void setDataTableList(DataModel dataTableList) {
		this.dataTableList = dataTableList;
	}

	/**
	 * @return Returns the filing.
	 */
	public Filing getFiling() {
		return filing;
	}

	/**
	 * @param filing The filing to set.
	 */
	public void setFiling(Filing filing) {
		this.filing = filing;
	}

	/**
	 * @return Returns the caseType
	 */
	public String getCaseType() {
		return caseType;
	}

	/**
	 * @param caseType The caseType to set
	 */
	public void setCaseType(String caseType) {
		this.caseType = caseType;
	}

	/**
	 * @return Returns the caseTypeUI
	 */
	public HtmlSelectOneListbox getCaseTypeUI() {
		return caseTypeUI;
	}

	/**
	 * @param caseTypeUI The caseTypeUI to set
	 */
	public void setCaseTypeUI(HtmlSelectOneListbox caseTypeUI) {
		this.caseTypeUI = caseTypeUI;
	}

	/**
	 * @return Returns the transmissionErrorCount
	 */
	public int getTransmissionErrorCount(){
		return transmissionErrorCount;
	}

	/**
	 * @param transmissionErrorCount The transmissionErrorCount to set.
	 */
	public void setTransmissionErrorCount(int transmissionErrorCount) {
		this.transmissionErrorCount = transmissionErrorCount;
	}

	/**
	 * This method navigates user to the transmissionErrorFilingPage
	 * @return String
	 */
	public String gotoTransmissionErrorFilingPage(){		 
		return "transmissionErrorFilingPage";
		 
	}
	
	/**
	 * Method to get the number of New Filings received.
	 * 
	 * @return int
	 */
	public int getNewFilingCount() {
		List<CaseRecord> cr = this.getModelFacade().findAllCaseRecordByStatus(1, courtUuid);
		return cr.size();
	}

	/**
	 * Method to get the number of Filings by Case Type
	 * "ChildSupportEnforcementFiling"
	 * 
	 * @return int
	 */
	public int getFilingCountByCaseTypeChildSupportEnforceMentFiling() {
		List<CaseRecord> cr = this.getModelFacade()
				.findAllCaseRecordByCaseType(1, courtUuid);
		return cr.size();
	}

	/**
	 * Method to get the number of Filings by Case Type
	 * "Civil"
	 * 
	 * @return int
	 */
	public int getFilingCountByCaseTypeCivil() {
		List<CaseRecord> cr = this.getModelFacade().findAllCaseRecordByCaseType(2, courtUuid);
		return cr.size();
	}

	/**
	 * Method to get the number of Filings by Case Type
	 * "Appellate"
	 * 
	 * @return int
	 */
	public int getFilingCountByCaseTypeAppellate() {
		List<CaseRecord> cr = this.getModelFacade().findAllCaseRecordByCaseType(1, courtUuid);
		return cr.size();
	}
	
	/**
	 * This method navigates user to the New Filings Page
	 * @return String
	 */
	public String gotoNewFiliingsPage() {
		String returnString = "newFilingPage";
		returnString = "newFilingPage";
		return returnString;
	}
	
	/**
	 * This method navigates user to the Recent Filings Page
	 * @return String
	 */
	public String gotoRecentFiliingsPage() {
		String returnString = "recentFilingPage";
		returnString = "recentFilingPage";
		return returnString;
	}

	/**
	 * This method navigates user to the corresponding page depending upon the 
	 * caseType of the case record
	 * @return String
	 */
	public String submitAction(){
		String navigationString = "";
		if (this.getCaseTypeUI().getValue().equals("appellate"))
			navigationString =  "appellate";
		else if (this.getCaseTypeUI().getValue().equals("civil"))
			navigationString =  "civil";
		else if (this.getCaseTypeUI().getValue().equals("childEnforcement"))
			navigationString ="gotoChildEnforcementCaseTypePage";
		return navigationString;
	}
	
	/**
	 * This method navigates user to the Case Details Page
	 * @return String
	 */
	public String gotoCaseDetailPage(){
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();
	 	this.filing = (Filing)this.getCaseDetailsPageClicked().getValue();
	 	String caseUuid = filing.getUuid();
	 	String caseType = filing.getCaseType();
	 	String toString="";
	 	if(caseType.equals("ChildSupportEnforcement"))
	 		toString = "gotoCSECaseDetailsPage";
	 	else toString = "gotoCaseDetailsPage";
	 	
	 	sessionMap.put("caseUuid",caseUuid);
	 	sessionMap.put("from", "MainPage");
	 	
	 	return toString;
	}
	
	/**
	 * This method loads data from database for status="Received"
	 *
	 */
	private void loadData(){
        //   status = received (1)
		List<Filing> newFilings = this.getModelFacade().findCaseFilingsByStatus(1, courtUuid);			
		dataTableList.setWrappedData(newFilings);
	}

}
